/**
 * @JsonBusiMixedDecoder.java  COPYRIGHT xiaojian
 * 
 * Created on 2017年7月20日 下午7:33:24
 */
package xj.toolkit.access.busi.codec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xj.toolkit.bean.CommonsHeader;
import xj.toolkit.bean.util.meta.Int2TypeMetainfo;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
public abstract class JsonBusiMixedDecoder extends BusiMixedDecoder {
	
	private Logger logger = LoggerFactory.getLogger(JsonBusiMixedDecoder.class);

	private Int2TypeMetainfo jsonTypeMetainfo;

	public Int2TypeMetainfo getJsonTypeMetainfo() {
		return jsonTypeMetainfo;
	}

	public void setJsonTypeMetainfo(Int2TypeMetainfo jsonTypeMetainfo) {
		this.jsonTypeMetainfo = jsonTypeMetainfo;
	}

	@Override
	protected Object parse(byte[] b, CommonsHeader header) throws Exception {
		int msgCode = header.getMessageCode();
		Class<?> clazz = jsonTypeMetainfo.find(msgCode);
		if (clazz == null) {
			throw new RuntimeException("invalid msgcode {" + msgCode + "}");
		}

		String jsonString = new String(b, "UTF-8");
		if (logger.isDebugEnabled() && isDebugEnabled) {
			logger.debug("jsonString-->" + jsonString);
		}
		
		return parse(jsonString, clazz);
	}

	protected abstract Object parse(String jsonString, Class<?> clazz);
}
